const playbackHelper = {
	
	/* Method to play a song uby playlist ID and makes the playlist of the current song as the current playlist.*/
	playSong: (playlistId,songId,isPlaybackStart) =>
	  {
	  	console.log('inside play song');
	    global.dataManager.getSoundtrackInfoById(playlistId,songId).then((soundTrackInfo) => {
	      global.playbackObject.currentSoundTrack = soundTrackInfo;
	      global.playbackObject.currentSongName = soundTrackInfo.title;
	      global.playbackObject.currentSoundTrackDescription = soundTrackInfo.notes;
	      console.log('Current soundTrack updated', global.playbackObject.currentSongName);
	      if(typeof isPlaybackStart == "undefined")
	      {
	      	global.playbackObject.isPlaying = true;
	      }else
	      {
	      	global.playbackObject.isPlaying = isPlaybackStart;
	      }
	      console.log('is playing', global.playbackObject.isPlaying);
	      playbackHelper.loadMusic(soundTrackInfo);
	      global.dataManager.getPlaylistInfoById(soundTrackInfo.playlistId).then((playlistInfo) => {
	        global.playbackObject.currentPlaylist = playlistInfo;
	        global.playbackObject.currentPlaylistName = playlistInfo.title;
	        console.log('Current playlist updated', global.playbackObject.currentPlaylist);
	      });
	    });
	    
	  },

	/*Pauses/Plays the music without changing the Icon in the  full audio player screen*/
	togglePlayPause: () =>
	  {
	  	if(typeof global.music!== "undefined")
    	{
		    if(global.playbackObject.isPlaying)
		    {
		      global.music.pause();
		      global.playbackObject.isPlaying = false;
		      global.playbackObject.playButton = 'pause';
		      return false;
		    }else
		    {
		      playbackHelper.startPlayback();
		      global.playbackObject.isPlaying = true;
		      global.playbackObject.playButton = 'play-arrow';
		      return true;
		    }
		}else
		{
			alert('No Audio File Loaded. Please select a song before playing');
		}
	  },
	  /*Starts playback of the currently playing song*/
	  startPlayback: () =>
	  {
	  	global.playbackObject.isPlaying = true;
	    global.music.play((success) => {
	        if (success) {
	          console.log('successfully finished playing');
	          playbackHelper.playNextSong();
	        } else {
	          console.log('playback failed due to audio decoding errors');
	          global.music.reset();
	        }
	      });
	  },

	 /*Plays the next song in the list*/
	 playNextSong: () =>
	  {
	    console.log('Playing next song');
	    console.log('Lenght of the playlist:',global.playbackObject.currentPlaylist.items.length);
	    console.log('musicCounter:', global.playbackObject.musicCounter);
	    var soundTrackId;
	    var playlistId;
	    var playback;
	    switch(global.playbackObject.currentRepeatMode)
      	{
	        case 0:
	        	if(++global.playbackObject.musicCounter>(global.playbackObject.currentPlaylist.items.length-1))
		          {
		          	global.playbackObject.musicCounter = (global.playbackObject.currentPlaylist.items.length-1);
		          	soundTrackId = global.playbackObject.currentPlaylist.items[global.playbackObject.musicCounter];
	          		global.playbackObject.indexOftheCurrentSoundTrack = global.playbackObject.musicCounter;
	          		playback = false;
		          }
		        if(global.playbackObject.isShuffle)
		        {
		          	var shuffledIndex = playbackHelper.getShuffledIndex();
		          	soundTrackId = global.playbackObject.currentPlaylist.items[shuffledIndex];
		          	global.playbackObject.indexOftheCurrentSoundTrack = shuffledIndex;
				}else
				{
				  soundTrackId = global.playbackObject.currentPlaylist.items[global.playbackObject.musicCounter];
				  global.playbackObject.indexOftheCurrentSoundTrack = global.playbackObject.musicCounter;
				}
			break;
	        case 1:
		          if(global.playbackObject.isShuffle)
		          {
		          	var shuffledIndex = playbackHelper.getShuffledIndex();
		          	soundTrackId = global.playbackObject.currentPlaylist.items[shuffledIndex];
		          	global.playbackObject.indexOftheCurrentSoundTrack = shuffledIndex;
		          }else
		          {
		          	if(++global.playbackObject.musicCounter>(global.playbackObject.currentPlaylist.items.length-1))
				    {
				      global.playbackObject.musicCounter = 0;
				    }
				    soundTrackId = global.playbackObject.currentPlaylist.items[global.playbackObject.musicCounter];
				    global.playbackObject.indexOftheCurrentSoundTrack = global.playbackObject.musicCounter;
		          }
	       	  break;
	        case 2:
	          soundTrackId = global.playbackObject.currentPlaylist.items[global.playbackObject.musicCounter];
	          global.playbackObject.indexOftheCurrentSoundTrack = global.playbackObject.musicCounter;
	          break;
      	} 
	    playbackHelper.playSong(global.playbackObject.currentPlaylist.uid,soundTrackId, playback);    

	  },

	 /*Plays the previous song in the list*/
	 playPreviousSong: () =>
	  {
	  	var soundTrackId;
	    var playlistId;
	    var playback;
	    switch(global.playbackObject.currentRepeatMode)
      	{
	        case 0:
	        	if(--global.playbackObject.musicCounter<0)
		          {
		          	// global.playbackObject.currentPlaylist = playbackHelper.getNextPlaylist();
		          	global.playbackObject.musicCounter = 0;
		          	soundTrackId = global.playbackObject.currentPlaylist.items[global.playbackObject.musicCounter];
	          		global.playbackObject.indexOftheCurrentSoundTrack = global.playbackObject.musicCounter;
	          		playback = false;
		          	console.log('Cuurent Playlist Set', global.playbackObject.currentPlaylist);
		          }
		        if(global.playbackObject.isShuffle)
		        {
		          	var shuffledIndex = playbackHelper.getShuffledIndex();
		          	soundTrackId = global.playbackObject.currentPlaylist.items[shuffledIndex];
		          	global.playbackObject.indexOftheCurrentSoundTrack = shuffledIndex;
				}else
				{
				  soundTrackId = global.playbackObject.currentPlaylist.items[global.playbackObject.musicCounter];
				  global.playbackObject.indexOftheCurrentSoundTrack = global.playbackObject.musicCounter;
				}
			break;
	        case 1:
		          if(global.playbackObject.isShuffle)
		          {
		          	var shuffledIndex = playbackHelper.getShuffledIndex();
		          	soundTrackId = global.playbackObject.currentPlaylist.items[shuffledIndex];
		          	global.playbackObject.indexOftheCurrentSoundTrack = shuffledIndex;
		          }else
		          {
		          	if(--global.playbackObject.musicCounter<0)
				    {
				      global.playbackObject.musicCounter = (global.playbackObject.currentPlaylist.items.length-1);
				    }
				    soundTrackId = global.playbackObject.currentPlaylist.items[global.playbackObject.musicCounter];
				    global.playbackObject.indexOftheCurrentSoundTrack = global.playbackObject.musicCounter;
		          }
	       	  break;
	        case 2:
	          soundTrackId = global.playbackObject.currentPlaylist.items[global.playbackObject.musicCounter];
	          global.playbackObject.indexOftheCurrentSoundTrack = global.playbackObject.musicCounter;
	          break;
      	} 
	    playbackHelper.playSong(global.playbackObject.currentPlaylist.uid,soundTrackId,playback);  
	  },

	  loadMusic:(soundTrackInfo) =>
	  {
	    if(global.music !== undefined)
	    {
	      global.music.stop();
	      global.music.release();
	  	}	

	    playbackHelper.initializePlayer(soundTrackInfo.path); 
	      // if(isPlaying)
	      // {
	      // this.waitAndStartPlayback();
	      // }
	  },
	  /*Initializes the player. Strictly to be used within this class*/
	  initializePlayer:(songName) =>
	  {
	    console.log('Loading: ',songName);

	   	// console.log('global.rnSound.Sound.LIBRARY:',global.rnSound.LIBRARY);
	   	// console.log('global.rnSound.Sound.MAIN_BUNDLE:',global.rnSound.MAIN_BUNDLE);
	    // console.log('global.rnSound.Sound.DOCUMENT:',global.rnSound.DOCUMENT);
	   	// console.log('global.rnSound.Sound.DOCUMENT:',global.rnSound.CACHES);

	    global.music = new rnSound(songName,'', (error) => {
	        if (error) {
	          console.log('failed to load the sound', error);
	          return;
	        }
	        console.log('Music loaded successfully');
	        if(global.playbackObject.isPlaying)
	        {
	          playbackHelper.startPlayback();
	        }
	        // global.playbackObject.currentSongName = songName;
	        // loaded successfully
	        console.log('Music loaded successfully');
	      });
	  },
	/*Sets the current time as current playback time in seconds + Delta in seconds. 
  	Delta should be in negative if playback time is to be reduced*/

  setCurrentTime:(deltaInCurrentTime) =>
  {
  	if(typeof global.music!== "undefined")
    {
	    global.music.getCurrentTime((seconds) => 
	    {
	      var targetTime = seconds+deltaInCurrentTime;
	      if(targetTime>global.music.getDuration())
	      {
	        global.music.setCurrentTime(global.music.getDuration());        
	      }else if(targetTime<1)
	      {
	        global.music.setCurrentTime(0);
	      }else{
	        global.music.setCurrentTime(targetTime);
	      }
	    }
	      );
	}else
	{
		alert('No Audio File Loaded. Please select a song before playing');
	}
  },
  /*Outputs the integer in two digit format*/
  displayTwoDigits:(n) =>
  {
    var displayNumber;
    if(n<10)
    {
      displayNumber = '0'+n;
    }else
    {
      displayNumber = n;
    }

    return displayNumber;
  },
  getRepeatIcon:() =>
	{
      var repeatIcon;
      switch(global.playbackObject.currentRepeatMode)
      {
        case 0:
          repeatIcon = 'repeat-off';
          break;
        case 1:
       	  repeatIcon = 'repeat';
       	  break;
        case 2:
          repeatIcon = 'repeat-once';
          break;
      }
      return repeatIcon;
	},

   getShuffleIcon:() =>
	{
  	  var shuffleIcon;
      if(global.playbackObject.isShuffle)
      {
        // shuffleIcon = 'shuffle';
        shuffleIcon = '#74F8F7';
      }else{
        // shuffleIcon= 'not-interested';
        shuffleIcon = '#666666';
      }
  	  return shuffleIcon;
	},

  getPlayPauseIcon:()=>
	{	
	  var playButtonIcon;
	  if(global.playbackObject.isPlaying)
	  {
	    playButtonIcon = 'pause';
	  }else{
	    playButtonIcon = 'play-arrow'
	  }
	  return playButtonIcon;
	},

  getShuffledIndex:() =>
	{
		var shuffledIndex = Math.trunc(Math.random()*(global.playbackObject.currentPlaylist.items.length));
		if((shuffledIndex==global.playbackObject.indexOftheCurrentSoundTrack)&&(global.playbackObject.currentPlaylist.items.length!=1))
		{
		    shuffledIndex= playbackHelper.getShuffledIndex();
		}
		return shuffledIndex;
	},
	
  getNextPlaylist:() =>
	{	
		var playlist;
		global.dataManager.getAllPlaylists().then ((playlistArray) => {
			for(let i=0;i<playlistArray.length;i++)
			{
				console.log('Iterating through.', playlistArray[i]);
				if(playlistArray[i].uid==global.playbackObject.currentPlaylist.uid)
				{
					if(i==(playlistArray.length-1))
					{
						playlist = playlistArray[0];
					}else
					{
						playlist = playlistArray[i+1];
					}
					break;
				}
			}
		});
		if(typeof playlist === "undefined")
		{
			playlist = global.playbackObject.currentPlaylist;
		}
		return playlist;
	}
}

export default playbackHelper;